#include "fontmanager.h"

CFontManager *CFontManager::getInstance()
{
    /**
     * @static
     * @return CFontManager* A pointer to the static singleton instance
     */
    static CFontManager s_instance;
    return &s_instance;
}

CFontManager::~CFontManager()
{
    /**
     * Destructor, ensure that we destruct all font objects.
     */
    for (map<string, CFont*>::const_iterator pFont(m_arrFonts.begin());
         pFont != m_arrFonts.end(); ++pFont)
    {
        delete pFont->second;
    }
    m_arrFonts.clear();
}

CFont *CFontManager::getFont(string szFontName) const
{
    /**
     * @param string szFontName The name of the font
     * @return CFont* a pointer to the font object
     */
    map<string, CFont*>::const_iterator pFont(m_arrFonts.find(szFontName));
    if (pFont == m_arrFonts.end())
    {
        return nullptr;
    }
    return pFont->second;
}

bool CFontManager::hasFont(string szFontName) const
{
    /**
     * @param string szFontName The name of the font
     * @return bool Whether or not the font name has been initialised and stored
     */
    return getFont(szFontName) != nullptr;
}

CFont *CFontManager::createFont(string szFontName, string szPath)
{
    /**
     * Creates the font if it doesn't already exist. If it does, then return 
     * the already created font.
     *
     * @param string szFontName The name of the font
     * @param string szPath The path to the font
     * @return CFont* A pointer to the font.
     */
    CFont *pFont(getFont(szFontName));
    if (pFont == nullptr)
    {
        pFont = new CFont(szFontName, szPath);
        m_arrFonts.insert(pair<string, CFont*>(szFontName, pFont));
    }
    return pFont;
}

CFont *CFontManager::createFont(string szKeyName, string szFontName, 
    string szPath)
{
    /**
     * Creates the font if it doesn't already exist. If it does, then return 
     * the already created font.
     *
     * @param string szKeyName The name of the key of the font
     * @param string szFontName The name of the font
     * @param string szPath The path to the font
     * @return CFont* A pointer to the font.
     */
    CFont *pFont(getFont(szKeyName));
    if (pFont == nullptr)
    {
        pFont = new CFont(szFontName, szPath);
        m_arrFonts.insert(pair<string, CFont*>(szKeyName, pFont));
    }
    return pFont;
}

CFont *CFontManager::createFont(string szKeyName, string szFontName, 
    string szPath, int nSize)
{
    /**
     * Creates the font if it doesn't already exist. If it does, then return 
     * the already created font.
     *
     * @param string szKeyName The name of the key of the font
     * @param string szFontName The name of the font
     * @param string szPath The path to the font
     * @param int nSize The size of teh font
     * @return CFont* A pointer to the font.
     */
    CFont *pFont(getFont(szKeyName));
    if (pFont == nullptr)
    {
        pFont = new CFont(szFontName, szPath, nSize);
        m_arrFonts.insert(pair<string, CFont*>(szKeyName, pFont));
    }
    return pFont;
}